package com.jcli.HslCommunication.HslCommunicationDemo;


import com.jcli.HslCommunication.Core.Net.IReadWriteNet;
import com.jcli.HslCommunication.Core.Types.OperateResult;
import com.jcli.HslCommunication.Core.Types.OperateResultExOne;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DemoUtils {

    private static SimpleDateFormat dateFormat = new SimpleDateFormat("[HH:mm:ss] ");

    /**
     * 用于处理读取操作的结果，将结果显示在文本框中。
     *
     * @param result     读取操作的结果，包含操作是否成功以及读取的内容。
     * @param address    读取操作的地址或标识。
     * @param textBox    显示读取结果的文本框。
     * @param scrollPane 滚动面板，用于滚动文本框中的内容。
     * @param <T>        读取的数据类型，可以是布尔、整数、浮点数等。
     */
    public static <T> void ReadResultRender(OperateResultExOne<T> result, String address, JTextArea textBox, JScrollPane scrollPane) {
        if (result.IsSuccess) {
            // 如果读取操作成功，将读取结果显示在文本框中，并滚动到最底部
            textBox.append(dateFormat.format(new Date()) + "[" + address + "] " + result.Content + "\r\n");
            JScrollBar scrollBar = scrollPane.getVerticalScrollBar();
            // 将垂直滚动条的值设置为其最大值，实现了在显示成功结果后将文本框滚动到底部。
            scrollBar.setValue(scrollBar.getMaximum());
        } else {
            // 如果读取操作失败，弹出警告框显示失败信息
            JOptionPane.showMessageDialog(
                    null,
                    dateFormat.format(new Date()) + "[" + address + "] Read Failed\r\nReason:" + result.ToMessageShowString(),
                    "Result",
                    JOptionPane.WARNING_MESSAGE);
        }
    }



    public static void WriteResultRender(OperateResult result, String address) {

        if (result.IsSuccess) {
            JOptionPane.showMessageDialog(
                    null,
                    dateFormat.format(new Date()) + "[" + address + "] Write Success",
                    "Result",
                    JOptionPane.PLAIN_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(
                    null,
                    dateFormat.format(new Date()) + "[" + address + "] Write Failed\r\nReason:" + result.ToMessageShowString(),
                    "Result",
                    JOptionPane.WARNING_MESSAGE);
        }
    }


    public static void BulkReadRenderResult(IReadWriteNet readWrite, JTextField addTextBox, JTextField lengthTextBox, JTextArea resultTextBox) {
        try {
            OperateResultExOne<byte[]> read = readWrite.Read(addTextBox.getText(), Short.parseShort(lengthTextBox.getText()));
            if (read.IsSuccess) {
                resultTextBox.setText("Result：" + com.jcli.HslCommunication.BasicFramework.SoftBasic.ByteToHexString(read.Content));
            } else {
                JOptionPane.showMessageDialog(null, "Read Failed：" + read.ToMessageShowString(), "Result", JOptionPane.WARNING_MESSAGE);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Read Failed：" + ex.getMessage(), "Result", JOptionPane.ERROR_MESSAGE);
        }
    }

    public static void SetPanelEnabled(JPanel panel, boolean enable) {
        Component[] comptents = panel.getComponents();

        //System.out.println("Component Length:" + comptents.length);
        for (Component component : comptents) {
            if (component instanceof JPanel) {
                SetPanelEnabled((JPanel) component, enable);
            } else {
                component.setEnabled(enable);
            }
        }

    }

    public static String IpAddressInputWrong = "IpAddress input wrong";
    public static String PortInputWrong = "Port input wrong";
    public static String SlotInputWrong = "Slot input wrong";
    public static String BaudRateInputWrong = "Baud rate input wrong";
    public static String DataBitsInputWrong = "Data bit input wrong";
    public static String StopBitInputWrong = "Stop bit input wrong";
}
